package com.aaron.kjviewutils.wechatsift;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aaron.kjviewutils.BaseActivity;
import com.aaron.kjviewutils.R;
import com.aaron.kjviewutils.footballleague.TabPagerStateAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：Aaron on 2017/2/20 15:38
 * 邮箱：kaijin.fang@edianniu.com
 */
public class WeChatSiftActivity extends BaseActivity {

    @Bind(R.id.weChatSiftToolBar)
    Toolbar weChatSiftToolBar;
    @Bind(R.id.barTitle)
    TextView barTitle;
    @Bind(R.id.leftBarIcon)
    ImageView leftBarIcon;
    @Bind(R.id.weChatSiftTabs)
    TabLayout weChatSiftTabs;
    @Bind(R.id.weChatSiftViewpager)
    ViewPager weChatSiftViewpager;

    public Toolbar toolBar;
    public WeChatBean data;
    private TabPagerStateAdapter adapter;
    private List<Fragment> fragments;

    @OnClick(R.id.leftBarIcon)
    void leftBarIconClick()
    {
        closeSelf(WeChatSiftActivity.this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.layout_wechat);
        ButterKnife.bind(this);
        data = (WeChatBean) this.getIntent().getExtras().getSerializable("data");
        initTitle();
        initContent();
    }

    private void initTitle()
    {
        toolBar = weChatSiftToolBar;
        weChatSiftToolBar.setTitle("");
        barTitle.setText("微信精选");
        loadBackground(leftBarIcon, resources.getDrawable(R.mipmap.back));
        weChatSiftToolBar.setNavigationIcon(null);
    }

    private void initContent()
    {
        fragments = new ArrayList<Fragment>();
        if(null!=data)
        {
            List<WeChatBean.WeChatCatagory> datas = data.result;
            if(null!=datas&&!datas.isEmpty())
            {
                for(WeChatBean.WeChatCatagory weChatCatagory:datas)
                {
                    WeChatFragment weChatFragment = new WeChatFragment();
                    Bundle bundle = new Bundle();
                    bundle.putString("cid", weChatCatagory.cid);
                    weChatFragment.setArguments(bundle);
                    fragments.add(weChatFragment);
                }
                adapter = new TabPagerStateAdapter(getSupportFragmentManager(), fragments);
                weChatSiftViewpager.setAdapter(adapter);
                weChatSiftTabs.setupWithViewPager(weChatSiftViewpager);
                weChatSiftTabs.setTabMode(TabLayout.MODE_SCROLLABLE);
                int size = datas.size();
                for(int i=0; i<size; i++)
                {
                    WeChatBean.WeChatCatagory weChatCatagory = datas.get(i);
                    //初始化tabs
                    TabLayout.Tab tab = weChatSiftTabs.getTabAt(i);
                    tab.setCustomView(getTabsItem(weChatCatagory.name));
                }
            }
        }
    }

    private View getTabsItem(String tag)
    {
        View view = inflater.inflate(R.layout.tas_item, null, false);
        TextView tabsTag = (TextView) view.findViewById(R.id.tabsTag);
        tabsTag.setText(tag);
        return view;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN)
        {
            //关闭
            closeSelf(WeChatSiftActivity.this);
        }
        return super.onKeyDown(keyCode, event);
    }
}
